﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using LamSon.Commons;
using Data.Controllers;

namespace LamSon.Controllers
{
    public class NhatKyBanHangController : BaseController
    {
        
        //
        // GET: /NhatKyBanHang/
        BaseController bs = new BaseController();
        public ActionResult NhatKyBanHang()
        {            
            return bs.BaseIndex(3,"NhatKyBanHang");
        }
        public static List<Data.DBOES.NhatKyBanHang> getListNKBH(string ngayban, string chuhang, string diachi, string phanloai,
            string thanhtoan, string xetangbo, string diadiemtangbo, string xevanchuyen, string sanpham,
            string nhapxuat, string bentravanchuyen, string khachhang, string ghichu,string tracuoctb,string tracuocvc)
        {
            Data.DBOES.NhatKyBanHang objCon=new Data.DBOES.NhatKyBanHang();
            DateTime dt;
            if (DateTime.TryParse(ngayban, out dt))
            {
                objCon.NgayBan = dt;
            }
            else
            {
                objCon.NgayBan = null;
            }
            objCon.ChuHang =string.IsNullOrEmpty(chuhang)?null:HttpUtility.HtmlEncode(chuhang);
            objCon.DiaChi = string.IsNullOrEmpty(diachi) ? null : HttpUtility.HtmlEncode(diachi);
            objCon.PL = string.IsNullOrEmpty(phanloai) ? null : HttpUtility.HtmlEncode(phanloai);
            if (!string.IsNullOrEmpty(thanhtoan))
            {
                objCon.TraTienMuoiNhap = LamSon.Commons.DataTypeProtect.ProtectBoolean(thanhtoan, false);
            }
            else
            {
                objCon.TraTienMuoiNhap = null;
            }
            
            if (!string.IsNullOrEmpty(tracuoctb))
            {
                objCon.TraCuocTB = LamSon.Commons.DataTypeProtect.ProtectBoolean(tracuoctb, false);
            }
            else
            {
                objCon.TraCuocTB = null;
            }
            
            if (!string.IsNullOrEmpty(tracuocvc))
            {
                objCon.TraCuocVC = LamSon.Commons.DataTypeProtect.ProtectBoolean(tracuocvc, false);
            }
            else
            {
                objCon.TraCuocVC = null;
            }
            objCon.SoXeTangBo = string.IsNullOrEmpty(xetangbo) ? null : HttpUtility.HtmlEncode(xetangbo);
            objCon.DiaDiemTangBo = string.IsNullOrEmpty(diadiemtangbo) ? null : HttpUtility.HtmlEncode(diadiemtangbo);
            objCon.SoXeVanChuyen = string.IsNullOrEmpty(xevanchuyen) ? null : HttpUtility.HtmlEncode(xevanchuyen);
            objCon.SanPham = string.IsNullOrEmpty(sanpham) ? null : HttpUtility.HtmlEncode(sanpham);
            objCon.NhapXuat = string.IsNullOrEmpty(nhapxuat) ? null : HttpUtility.HtmlEncode(nhapxuat);
            objCon.BenTraVanChuyen = string.IsNullOrEmpty(bentravanchuyen) ? null : HttpUtility.HtmlEncode(bentravanchuyen);
            if (!string.IsNullOrEmpty(khachhang))
            {
                objCon.IdKhachHang = LamSon.Commons.DataTypeProtect.ProtectInt32(khachhang);
            }
            else
            {
                objCon.IdKhachHang = null;
            }
            
            objCon.GhiChu = string.IsNullOrEmpty(ghichu) ? null : HttpUtility.HtmlEncode(ghichu);
            List<Data.DBOES.NhatKyBanHang> objList = Data.Controllers.NhatKyBanHangController.FindItems(objCon, BaseController.Pagecurr, BaseController.LimitPage, ref BaseController.totalRow);
            return objList;
        }
        
        public static List<Data.DBOES.NhatKyBanHang> bindComboxSearch(string fielName)
        {
            List<Data.DBOES.NhatKyBanHang> objListAll = new List<Data.DBOES.NhatKyBanHang>();
            List<Data.DBOES.NhatKyBanHang> objList = Data.Controllers.NhatKyBanHangController.NhatKyBanHang_Select_Distinct("NhatKyBanHang_Select_Distinct", fielName);
            Data.DBOES.NhatKyBanHang obj = new Data.DBOES.NhatKyBanHang();
            obj.NgayBan = null;
            obj.ChuHang = null;
            obj.DiaChi = null;
            obj.PL = null;
            obj.SoXeTangBo = null;
            obj.DiaDiemTangBo = null;
            obj.SoXeVanChuyen = null;
            obj.SanPham = null;
            obj.NhapXuat = null;
            obj.IdKhachHang = null;
            obj.BenTraVanChuyen = null;
            obj.NoThu = null;
            obj.GhiChu = null;
            
            objListAll.Add(obj);
            for (int i = 0; i < objList.Count; i++)
            {
                objList[i].ChuHang = HttpUtility.HtmlDecode(objList[i].ChuHang);
                objList[i].DiaChi = HttpUtility.HtmlDecode(objList[i].DiaChi);
                objList[i].DiaDiemTangBo = HttpUtility.HtmlDecode(objList[i].DiaDiemTangBo);
                objList[i].BenTraVanChuyen = HttpUtility.HtmlDecode(objList[i].BenTraVanChuyen);
                objList[i].GhiChu = HttpUtility.HtmlDecode(objList[i].GhiChu);
                
                objListAll.Add(objList[i]);
            }
            return objListAll;
        }
        
        public static List<Data.DBOES.Membership> getListCustomer(int idrole)
        {
            Data.DBOES.Membership objCondition = new Data.DBOES.Membership();            
            objCondition.IsActive = true;
            objCondition.IdRole = idrole;
            objCondition.SortBy = "MembershipName";
            objCondition.SortType = Core.SortType.ASC;
            List<Data.DBOES.Membership> objCustommerListAll = new List<Data.DBOES.Membership>();
            Data.DBOES.Membership obj = new Data.DBOES.Membership();
            obj.IsActive = true;
            obj.IdRole = 5;
            obj.MembershipName = "";
            obj.IdMembership = null;
            objCustommerListAll.Add(obj);

            List<Data.DBOES.Membership> objCustommerList = MembershipController.FindItems(objCondition);

            if (objCustommerList != null)
            {
                for (int i = 0; i < objCustommerList.Count; i++)
                {
                    objCustommerList[i].MembershipName = HttpUtility.HtmlDecode(objCustommerList[i].MembershipName);
                    objCustommerListAll.Add(objCustommerList[i]);
                }
            }
            return objCustommerListAll;
        }
        //
        // GET: /NhatKyBanHang/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }
        
        //
        // GET: /NhatKyBanHang/Create
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Update(string IdNKBH,string NgayBan,string ChuHang,string DiaChi,string TraTienMuoiNhap,string TraCuocTB,string TraCuocVC,
            string XeTangBo,string CuocXeTangBo,string DiemTangBo,string SoLuong,string XeVanChuyen,
                string CuocVanChuyen,string SanPham,string GiaNhap,string PhanLoai,string KhachHang,
                    string GiaXuat, string BenTraVanChuyen, string ChuThich, string BBBV,string ThanhToanTruoc,string HinhThucTT,
            string ngayban_edit, string chuhang_edit, string diachi_edit, string phanloai_edit,
            string thanhtoan_edit, string xetangbo_edit, string diadiemtangbo_edit, string xevanchuyen_edit, string sanpham_edit,
            string nhapxuat_edit, string bentravanchuyen_edit, string khachhang_edit, string ghichu_edit, string tracuoctb_edit, string tracuocvc_edit, string pagesize_edit, string page_edit
            )
        {
            Data.DBOES.NhatKyBanHang objCon;
            if(!string.IsNullOrEmpty(IdNKBH))
            {
                objCon=Data.Controllers.NhatKyBanHangController.FindItem(new Data.DBOES.NhatKyBanHang(Commons.DataTypeProtect.ProtectInt32(IdNKBH)));
            }
            else
            {
                objCon= new Data.DBOES.NhatKyBanHang();
            }
            objCon.BenTraVanChuyen =HttpUtility.HtmlEncode(Commons.Commons.RemoveHtmlTag(BenTraVanChuyen));
            objCon.ChuHang = HttpUtility.HtmlEncode(Commons.Commons.RemoveHtmlTag(ChuHang));
            objCon.CuocXeTangBo = Commons.DataTypeProtect.ProtectDouble(Commons.Commons.RemoveHtmlTag(CuocXeTangBo),0);
            objCon.CuocXeVanChuyen = Commons.DataTypeProtect.ProtectDouble(Commons.Commons.RemoveHtmlTag(CuocVanChuyen),0);
            objCon.DiaChi = HttpUtility.HtmlEncode(Commons.Commons.RemoveHtmlTag(DiaChi));
            objCon.DiaDiemTangBo = HttpUtility.HtmlEncode(Commons.Commons.RemoveHtmlTag(DiemTangBo));
            objCon.GhiChu = HttpUtility.HtmlEncode(Commons.Commons.RemoveHtmlTag(ChuThich));
            objCon.GiaBaoBiBocVac = Commons.DataTypeProtect.ProtectDouble(Commons.Commons.RemoveHtmlTag(BBBV),0);
            objCon.GiaNhap = Commons.DataTypeProtect.ProtectDouble(Commons.Commons.RemoveHtmlTag(GiaNhap),0);
            objCon.GiaXuat = Commons.DataTypeProtect.ProtectDouble(Commons.Commons.RemoveHtmlTag(GiaXuat),0);
            objCon.IdKhachHang = Commons.DataTypeProtect.ProtectInt32(Commons.Commons.RemoveHtmlTag(KhachHang));
            objCon.ThanhToanTruoc = Commons.DataTypeProtect.ProtectDouble(Commons.Commons.RemoveHtmlTag(ThanhToanTruoc), 0);
            objCon.HinhThucTT = HttpUtility.HtmlEncode(Commons.Commons.RemoveHtmlTag(HinhThucTT));
            if (!string.IsNullOrEmpty(NgayBan))
            {
                DateTime ngayban;
                if (!DateTime.TryParse(NgayBan.Split('/')[1] + "/" + NgayBan.Split('/')[0] + "/" + NgayBan.Split('/')[2], out ngayban))
                {
                    
                }
                objCon.NgayBan = ngayban;
            }
            else
                objCon.NgayBan = null;

            if (!string.IsNullOrEmpty(GiaNhap) && string.IsNullOrEmpty(GiaXuat) && DataTypeProtect.ProtectInt32(GiaNhap) != 0)
                objCon.NhapXuat = "Nhập";
            if (!string.IsNullOrEmpty(GiaNhap) && !string.IsNullOrEmpty(GiaXuat) && DataTypeProtect.ProtectInt32(GiaNhap) != 0 && DataTypeProtect.ProtectInt32(GiaXuat) == 0)
                objCon.NhapXuat = "Nhập";

            if (string.IsNullOrEmpty(GiaNhap) && !string.IsNullOrEmpty(GiaXuat) && DataTypeProtect.ProtectInt32(GiaXuat) != 0)
                objCon.NhapXuat = "Xuất";
            if (!string.IsNullOrEmpty(GiaNhap) && !string.IsNullOrEmpty(GiaXuat) && DataTypeProtect.ProtectInt32(GiaNhap) == 0 && DataTypeProtect.ProtectInt32(GiaXuat) != 0)
                objCon.NhapXuat = "Xuất";
            if (!string.IsNullOrEmpty(GiaNhap) && !string.IsNullOrEmpty(GiaXuat) && DataTypeProtect.ProtectInt32(GiaNhap) != 0 && DataTypeProtect.ProtectInt32(GiaXuat) != 0)
                objCon.NhapXuat = "N/X";
            if (BenTraVanChuyen == "Công ty Lam Sơn")
                objCon.NoThu = DataTypeProtect.ProtectString(DataTypeProtect.ProtectDouble(SoLuong, 0) * DataTypeProtect.ProtectDouble(GiaXuat, 0));
            if (BenTraVanChuyen == "Khách Hàng")
                objCon.NoThu = DataTypeProtect.ProtectString((DataTypeProtect.ProtectDouble(GiaXuat, 0) - DataTypeProtect.ProtectDouble(CuocVanChuyen, 0)) * DataTypeProtect.ProtectDouble(SoLuong, 0));
            objCon.PL = HttpUtility.HtmlEncode(Commons.Commons.RemoveHtmlTag(PhanLoai));
            objCon.SanPham=HttpUtility.HtmlEncode(Commons.Commons.RemoveHtmlTag(SanPham));
            objCon.SoLuong = Commons.DataTypeProtect.ProtectDouble(Commons.Commons.RemoveHtmlTag(SoLuong),0);
            objCon.SoXeTangBo = HttpUtility.HtmlEncode(Commons.Commons.RemoveHtmlTag(XeTangBo));
            objCon.SoXeVanChuyen = HttpUtility.HtmlEncode(Commons.Commons.RemoveHtmlTag(XeVanChuyen));
            objCon.TraTienMuoiNhap = Commons.DataTypeProtect.ProtectBoolean(TraTienMuoiNhap, false);
            objCon.TraCuocTB = Commons.DataTypeProtect.ProtectBoolean(TraCuocTB, false);
            objCon.TraCuocVC = Commons.DataTypeProtect.ProtectBoolean(TraCuocVC, false);
            if (!string.IsNullOrEmpty(IdNKBH))
            {
                objCon.IsActive = false;
                objCon.Update();
            }
            else
            {
                objCon.IsActive = true;
                objCon.Insert();
            }
            BaseController.LimitPage = Commons.DataTypeProtect.ProtectInt32(string.IsNullOrEmpty(pagesize_edit) ? "50" : pagesize_edit);
            BaseController.Pagecurr = Commons.DataTypeProtect.ProtectInt32(string.IsNullOrEmpty(page_edit) ? "1" : page_edit);
            ReturnViewData_NKBH(ngayban_edit, chuhang_edit, diachi_edit, phanloai_edit,
            thanhtoan_edit, xetangbo_edit, diadiemtangbo_edit, xevanchuyen_edit, sanpham_edit,
            nhapxuat_edit, bentravanchuyen_edit, khachhang_edit, ghichu_edit,tracuoctb_edit,tracuocvc_edit);
            return PartialView("ListNKBH");
        }

        public void getTotalMoney(string ngayban, string chuhang, string diachi, string phanloai,
            string thanhtoan, string xetangbo, string diadiemtangbo, string xevanchuyen, string sanpham,
            string nhapxuat, string bentravanchuyen, string khachhang, string ghichu, string tracuoctb, string tracuocvc)
        {
            double totalCuocXeTangBo = 0;
            double totalTienMuoiNhap = 0;
            double totalTTCuocTB = 0;
            double totalSoLuong = 0;
            double totalTTCuocVC = 0;
            double totalBBBV = 0;
            double totalTTNhap = 0;
            double totalTTXuat = 0;
            double totalTienMuoiXuat = 0;
            double totalDoanhThu = 0;
            double totalNoThu = 0;
            List<Data.DBOES.NhatKyBanHang> objNhatKyBanHangListAll = new List<Data.DBOES.NhatKyBanHang>();
            Data.DBOES.NhatKyBanHang objCon = new Data.DBOES.NhatKyBanHang();
            if (!string.IsNullOrEmpty(ngayban))
            {
                DateTime nb;
                if (!DateTime.TryParse(ngayban, out nb))
                {
                    objCon.NgayBan = nb;
                }
                else
                {
                    objCon.NgayBan = null;
                }
            }            
            if (!string.IsNullOrEmpty(chuhang))
            {
                objCon.ChuHang =HttpUtility.HtmlEncode(chuhang);
            }
            if (!string.IsNullOrEmpty(diachi))
            {
                objCon.DiaChi = HttpUtility.HtmlEncode(diachi);
            }
            if (!string.IsNullOrEmpty(phanloai))
            {
                objCon.PL = HttpUtility.HtmlEncode(phanloai);
            }
            if (!string.IsNullOrEmpty(thanhtoan))
            {
                objCon.TraTienMuoiNhap =LamSon.Commons.DataTypeProtect.ProtectBoolean(thanhtoan,false);
            }
            if (!string.IsNullOrEmpty(xetangbo))
            {
                objCon.SoXeTangBo = HttpUtility.HtmlEncode(xetangbo);
            }
            if (!string.IsNullOrEmpty(diadiemtangbo))
            {
                objCon.DiaDiemTangBo = HttpUtility.HtmlEncode(diadiemtangbo);
            }
            if (!string.IsNullOrEmpty(xevanchuyen))
            {
                objCon.SoXeVanChuyen = HttpUtility.HtmlEncode(xevanchuyen);
            }
            if (!string.IsNullOrEmpty(sanpham))
            {
                objCon.SanPham = HttpUtility.HtmlEncode(sanpham);
            }
            if (!string.IsNullOrEmpty(nhapxuat))
            {
                objCon.NhapXuat = HttpUtility.HtmlEncode(nhapxuat);
            }
            if (!string.IsNullOrEmpty(bentravanchuyen))
            {
                objCon.BenTraVanChuyen = HttpUtility.HtmlEncode(bentravanchuyen);
            }
            if (!string.IsNullOrEmpty(khachhang))
            {
                objCon.IdKhachHang=LamSon.Commons.DataTypeProtect.ProtectInt32(khachhang);
            }
            if (!string.IsNullOrEmpty(ghichu))
            {
                objCon.GhiChu = HttpUtility.HtmlEncode(ghichu);
            }
            if (!string.IsNullOrEmpty(tracuoctb))
            {
                objCon.TraCuocTB = LamSon.Commons.DataTypeProtect.ProtectBoolean(tracuoctb, false);
            }
            if (!string.IsNullOrEmpty(tracuocvc))
            {
                objCon.TraCuocVC = LamSon.Commons.DataTypeProtect.ProtectBoolean(tracuocvc, false);
            }
            objNhatKyBanHangListAll = Data.Controllers.NhatKyBanHangController.FindItems(objCon);
            foreach (Data.DBOES.NhatKyBanHang nkbh in objNhatKyBanHangListAll)
            {
                totalCuocXeTangBo += DataTypeProtect.ProtectDouble(nkbh.CuocXeTangBo, 0);
                totalTienMuoiNhap += (DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0) * DataTypeProtect.ProtectDouble(nkbh.GiaNhap, 0));
                totalTTCuocTB += (DataTypeProtect.ProtectDouble(nkbh.CuocXeTangBo, 0) * DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0));
                totalSoLuong += DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0);
                totalTTCuocVC += DataTypeProtect.ProtectDouble(nkbh.CuocXeVanChuyen, 0) * DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0);
                totalBBBV += DataTypeProtect.ProtectDouble(nkbh.GiaBaoBiBocVac, 0);
                totalTTNhap += ((DataTypeProtect.ProtectDouble(nkbh.CuocXeTangBo, 0) + DataTypeProtect.ProtectDouble(nkbh.GiaNhap, 0) + DataTypeProtect.ProtectDouble(nkbh.GiaBaoBiBocVac, 0)) * DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0));
                totalTTXuat += (DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0) * DataTypeProtect.ProtectDouble(nkbh.GiaXuat, 0));
                totalTienMuoiXuat += ((DataTypeProtect.ProtectDouble(nkbh.GiaXuat, 0) - DataTypeProtect.ProtectDouble(nkbh.CuocXeVanChuyen, 0)) * DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0));
                totalDoanhThu += ((DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0) * DataTypeProtect.ProtectDouble(nkbh.GiaXuat, 0)) - ((DataTypeProtect.ProtectDouble(nkbh.CuocXeTangBo, 0) + DataTypeProtect.ProtectDouble(nkbh.GiaNhap, 0) + DataTypeProtect.ProtectDouble(nkbh.GiaBaoBiBocVac, 0)) * DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0)) - (DataTypeProtect.ProtectDouble(nkbh.SoLuong, 0) * DataTypeProtect.ProtectDouble(nkbh.CuocXeVanChuyen, 0)));
                totalNoThu += DataTypeProtect.ProtectDouble(nkbh.NoThu, 0);
            }
            ViewData["LabCuocXeTangBo"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalCuocXeTangBo));
            ViewData["LabSoLuong"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalSoLuong));
            ViewData["LabTTCuocVC"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalTTCuocVC));
            ViewData["LabTTNhap"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalTTNhap));
            ViewData["LabTTXuat"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalTTXuat));
            ViewData["LabTienMuoiXuat"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalTienMuoiXuat));
            ViewData["LabTienMuoiNhap"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalTienMuoiNhap));
            ViewData["LabDoanhThu"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalDoanhThu));
            ViewData["LabTTCuocTB"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalTTCuocTB));
            ViewData["LabNoThu"] = LamSon.Commons.Commons.FormatMoney(DataTypeProtect.ProtectString(totalNoThu));
        }
        //
        // GET: /NhatKyBanHang/Delete/5
 
        public ActionResult Delete(string idNKBH)
        {
            Data.DBOES.NhatKyBanHang objCon = Data.Controllers.NhatKyBanHangController.FindItem(new Data.DBOES.NhatKyBanHang(Commons.DataTypeProtect.ProtectInt32(idNKBH)));
            if (objCon != null)
                objCon.Delete();
            ReturnViewData_NKBH(null, null, null, null, null, null, null, null, null, null, null, null, null,null,null);
            return PartialView("ListNKBH");
        }
        public ActionResult Edit(string idNKBH_Edit,string ngayban_sendedit, string chuhang_sendedit, string diachi_sendedit, string phanloai_sendedit,
            string thanhtoan_sendedit, string xetangbo_sendedit, string diadiemtangbo_sendedit, string xevanchuyen_sendedit, string sanpham_sendedit,
            string nhapxuat_sendedit, string bentravanchuyen_sendedit, string khachhang_sendedit, string ghichu_sendedit, string tracuoctb_sendedit, string tracuocvc_sendedit, string pagesize_sendedit, string page_sendedit)
        { 
            Data.DBOES.NhatKyBanHang objCon = Data.Controllers.NhatKyBanHangController.FindItem(new Data.DBOES.NhatKyBanHang(Commons.DataTypeProtect.ProtectInt32(idNKBH_Edit)));
            ViewData["DataNKBH"]=objCon;
            ViewData["KhachHang"] = getListCustomer(5);
            SetViewDataInfoSearch(ngayban_sendedit, chuhang_sendedit, diachi_sendedit, phanloai_sendedit,
            thanhtoan_sendedit, xetangbo_sendedit, diadiemtangbo_sendedit, xevanchuyen_sendedit, sanpham_sendedit,
            nhapxuat_sendedit, bentravanchuyen_sendedit, khachhang_sendedit, ghichu_sendedit,tracuoctb_sendedit,tracuocvc_sendedit, pagesize_sendedit, page_sendedit);
            return PartialView("FormNhatKyBanHang");
           
        }
        public void SetViewDataInfoSearch(string ngayban_sendedit, string chuhang_sendedit, string diachi_sendedit, string phanloai_sendedit,
            string thanhtoan_sendedit, string xetangbo_sendedit, string diadiemtangbo_sendedit, string xevanchuyen_sendedit, string sanpham_sendedit,
            string nhapxuat_sendedit, string bentravanchuyen_sendedit, string khachhang_sendedit, string ghichu_sendedit, string tracuoctb_sendedit, string tracuocvc_sendedit, string pagesize_sendedit, string page_sendedit)
        {
            ViewData["ngayban_sendedit"] = ngayban_sendedit;
            ViewData["chuhang_sendedit"] = chuhang_sendedit;
            ViewData["diachi_sendedit"] = diachi_sendedit;
            ViewData["phanloai_sendedit"] = phanloai_sendedit;
            ViewData["thanhtoan_sendedit"] = thanhtoan_sendedit;
            ViewData["xetangbo_sendedit"] = xetangbo_sendedit;
            ViewData["diadiemtangbo_sendedit"] = diadiemtangbo_sendedit;
            ViewData["xevanchuyen_sendedit"] = xevanchuyen_sendedit;
            ViewData["sanpham_sendedit"] = sanpham_sendedit;
            ViewData["nhapxuat_sendedit"] = nhapxuat_sendedit;
            ViewData["bentravanchuyen_sendedit"] = bentravanchuyen_sendedit;
            ViewData["khachhang_sendedit"] = khachhang_sendedit;
            ViewData["ghichu_sendedit"] = ghichu_sendedit;
            ViewData["pagesize_sendedit"] = pagesize_sendedit;
            ViewData["page_sendedit"] = page_sendedit;
            ViewData["tracuoctb_sendedit"] = tracuoctb_sendedit;
            ViewData["tracuocvc_sendedit"] = tracuocvc_sendedit;
        }
        public ActionResult OK(string idNKBH_OK)
        {
            Data.DBOES.NhatKyBanHang objCon = Data.Controllers.NhatKyBanHangController.FindItem(new Data.DBOES.NhatKyBanHang(Commons.DataTypeProtect.ProtectInt32(idNKBH_OK)));
            if (objCon != null)
            {
                objCon.IsActive = true;
                objCon.Update();
            }
            ReturnViewData_NKBH(null, null, null, null, null, null, null, null, null, null, null, null, null,null,null);
            return PartialView("ListNKBH");
        }
        public ActionResult LoadLimitPage(string pagesize, string ngayban_limit, string chuhang_limit, string diachi_limit, string phanloai_limit,
            string thanhtoan_limit, string xetangbo_limit, string diadiemtangbo_limit, string xevanchuyen_limit, string sanpham_limit,
            string nhapxuat_limit, string bentravanchuyen_limit, string khachhang_limit, string ghichu_limit, string tracuoctb_limit,string tracuocvc_limit)
        {
            BaseController.LimitPage = LamSon.Commons.DataTypeProtect.ProtectInt32(pagesize);
            BaseController.Pagecurr = 1;
            ReturnViewData_NKBH(ngayban_limit, chuhang_limit, diachi_limit, phanloai_limit,
            thanhtoan_limit, xetangbo_limit, diadiemtangbo_limit, xevanchuyen_limit, sanpham_limit,
            nhapxuat_limit, bentravanchuyen_limit, khachhang_limit, ghichu_limit,tracuoctb_limit,tracuocvc_limit);
            return PartialView("ListNKBH");
        }
        public ActionResult LoadPage(string page, string ngayban_paging, string chuhang_paging, string diachi_paging, string phanloai_paging,
            string thanhtoan_paging, string xetangbo_paging, string diadiemtangbo_paging, string xevanchuyen_paging, string sanpham_paging,
            string nhapxuat_paging, string bentravanchuyen_paging, string khachhang_paging, string ghichu_paging, string tracuoctb_paging, string tracuocvc_paging)
        {
            BaseController.Pagecurr = LamSon.Commons.DataTypeProtect.ProtectInt32(page);
            ReturnViewData_NKBH(ngayban_paging, chuhang_paging, diachi_paging, phanloai_paging,
            thanhtoan_paging, xetangbo_paging, diadiemtangbo_paging, xevanchuyen_paging, sanpham_paging,
            nhapxuat_paging, bentravanchuyen_paging, khachhang_paging, ghichu_paging,tracuoctb_paging,tracuocvc_paging);
            return PartialView("ListNKBH");
        }
        private void ReturnViewData_NKBH(string ngayban, string chuhang, string diachi, string phanloai,
            string thanhtoan, string xetangbo, string diadiemtangbo, string xevanchuyen, string sanpham,
            string nhapxuat, string bentravanchuyen, string khachhang, string ghichu, string tracuoctb, string tracuocvc)
        {
            ViewData["IdForm"] = 3;
            ViewData["ListNKBH"] = NhatKyBanHangController.getListNKBH(ngayban,chuhang,diachi,phanloai,
            thanhtoan,xetangbo,diadiemtangbo,xevanchuyen,sanpham,
            nhapxuat,bentravanchuyen,khachhang,ghichu,tracuoctb,tracuocvc);
            ViewData["PageCurr"] = BaseController.Pagecurr;
            ViewData["LimitPage"] = BaseController.LimitPage;
            if (totalRow % LimitPage == 0)
            {
                ViewData["TotalPage"] = totalRow / LimitPage;
            }
            else
            {
                ViewData["TotalPage"] = totalRow / LimitPage + 1;
            }
        }
        public ActionResult LoadPageSearch(string ngayban,string chuhang,string diachi,string phanloai,
            string thanhtoan,string xetangbo,string diadiemtangbo,string xevanchuyen,string sanpham,
            string nhapxuat, string bentravanchuyen, string khachhang, string ghichu, string tracuoctb, string tracuocvc)
        {
            BaseController.Pagecurr = 1;
            ReturnViewData_NKBH(ngayban, chuhang, diachi, phanloai,
            thanhtoan, xetangbo, diadiemtangbo, xevanchuyen, sanpham,
            nhapxuat, bentravanchuyen, khachhang, ghichu,tracuoctb,tracuocvc);
            return PartialView("ListNKBH");
        }
        public ActionResult LoadPaging()
        {
            if (totalRow % LimitPage == 0)
            {
                ViewData["TotalPage"] = totalRow / LimitPage;
            }
            else
            {
                ViewData["TotalPage"] = totalRow / LimitPage + 1;
            }
            ViewData["PageCurr"] = BaseController.Pagecurr;
            
            return PartialView("Paging");
        }
        public string LoadTotalMoney(string ngayban_totalmoney, string chuhang_totalmoney, string diachi_totalmoney, string phanloai_totalmoney,
            string thanhtoan_totalmoney, string xetangbo_totalmoney, string diadiemtangbo_totalmoney, string xevanchuyen_totalmoney, string sanpham_totalmoney,
            string nhapxuat_totalmoney, string bentravanchuyen_totalmoney, string khachhang_totalmoney, string ghichu_totalmoney, string tracuoctb_totalmoney, string tracuocvc_totalmoney)
        {
            getTotalMoney(ngayban_totalmoney, chuhang_totalmoney, diachi_totalmoney, phanloai_totalmoney,
            thanhtoan_totalmoney, xetangbo_totalmoney, diadiemtangbo_totalmoney, xevanchuyen_totalmoney, sanpham_totalmoney,
            nhapxuat_totalmoney, bentravanchuyen_totalmoney, khachhang_totalmoney, ghichu_totalmoney,tracuoctb_totalmoney,tracuocvc_totalmoney);
            return ViewData["LabTienMuoiNhap"] + "*" + 
                ViewData["LabCuocXeTangBo"] + "*" +
                ViewData["LabTTCuocTB"] + "*" + 
                ViewData["LabSoLuong"] + "*" + 
                ViewData["LabTTCuocVC"] + "*" +
                ViewData["LabTTNhap"] + "*" +
                ViewData["LabTTXuat"] + "*" +
                ViewData["LabNoThu"]+ "*" + 
                ViewData["LabTienMuoiXuat"] + "*" +
                
                ViewData["LabDoanhThu"];
        }
        public ActionResult TraTienMuoiNhap(string IdNKBH_TraTienMuoiNhap, string TraTienMuoiNhap)
        {
            Data.DBOES.NhatKyBanHang objCon = Data.Controllers.NhatKyBanHangController.FindItem(new Data.DBOES.NhatKyBanHang(LamSon.Commons.DataTypeProtect.ProtectInt32(IdNKBH_TraTienMuoiNhap)));
            if (objCon != null)
            {
                objCon.TraTienMuoiNhap = LamSon.Commons.DataTypeProtect.ProtectBoolean(TraTienMuoiNhap,false);
                objCon.Update();
            }
            return null;
        }
        public ActionResult TraCuocTB(string IdNKBH_TraCuocTB,string TraCuocTB)
        {
            Data.DBOES.NhatKyBanHang objCon = Data.Controllers.NhatKyBanHangController.FindItem(new Data.DBOES.NhatKyBanHang(LamSon.Commons.DataTypeProtect.ProtectInt32(IdNKBH_TraCuocTB)));
            if (objCon != null)
            {
                objCon.TraCuocTB = LamSon.Commons.DataTypeProtect.ProtectBoolean(TraCuocTB, false);
                objCon.Update();
            }
            return null;
        }
        public ActionResult TraCuocVC(string IdNKBH_TraCuocVC,string TraCuocVC)
        {
            Data.DBOES.NhatKyBanHang objCon = Data.Controllers.NhatKyBanHangController.FindItem(new Data.DBOES.NhatKyBanHang(LamSon.Commons.DataTypeProtect.ProtectInt32(IdNKBH_TraCuocVC)));
            if (objCon != null)
            {
                objCon.TraCuocVC = LamSon.Commons.DataTypeProtect.ProtectBoolean(TraCuocVC, false);
                objCon.Update();
            }
            return null;
        }
    }
}
